package impl.repository;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import dao.AccessDatabase;
import domain.Billing;
import domain.Customer;
import domain.Order;
import domain.Shipping;
import interfaces.repository.OrderRepository;

public class StubOrderRepositoryImpl implements OrderRepository{

	
	@Override
	public Order findByNameCustomer(int idCustomer) {
		// TODO Auto-generated method stub
		Order order;
		Customer customer = null;
		Shipping shipping = null;
		Billing billing = null;
		try{

        	AccessDatabase a = new AccessDatabase();
            Connection con = a.conectar();
            Statement st = con.createStatement();
           	ResultSet rs = st.executeQuery("SELECT * FROM order where idCustomer='"+idCustomer+"';");
           
                
           			order = new Order (
	    				customer,
	    				shipping,
	    				billing
	                    );
                	
                
                con.close();
                return order ;
            
            } catch (SQLException ex) {
            	return null;
            }      
	}

	@Override
	public List<Order> findByValue(float value) {
		ArrayList<Order>orders = new ArrayList<Order>(); 
		Order order;
		
		Customer customer = null;
		Shipping shipping = null;
		Billing billing = null;
		
		try{

        	AccessDatabase a = new AccessDatabase();
            Connection con = a.conectar();
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery("SELECT * FROM order where name='"+value+"';");
            	while(rs.next()){
            		order = new Order (
            				customer,
            				shipping,
            				billing
                            );
            		orders.add(order);
            		} con.close();
            		return orders;
		} catch (SQLException ex){
			return null;
		}
		
		
	}

	@Override
	public boolean add(Order order) {
	try{ 
		AccessDatabase a = new AccessDatabase();
		Connection con = a.conectar();
		Statement st = con.createStatement();
		st.execute("INSERT INTO 'order' ('id', 'customer', 'billing', 'shipping') VALUES ('"+order.getId()+"', '"+order.getCustomer()+"', '"+order.getBilling()+"', '"+order.getShipping()+"');");
				con.close();
		return true;
	}catch (Exception t){
		return false;
	}
	
	}
		
}                                                                                          
           
            
              
	
	
	
	


